package com.free.semantic.model.util;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串工具类
 */
public class StringUtils {

    /**
     * 判断字符串是否为空或null
     *
     * @param str 要检查的字符串
     * @return 如果字符串为null或空字符串，则返回true
     */
    public static boolean isEmpty(String str) {
        return str == null || str.isEmpty();
    }

    /**
     * 判断字符串是否不为空且不为null
     *
     * @param str 要检查的字符串
     * @return 如果字符串不为null且不为空字符串，则返回true
     */
    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    /**
     * 替换字符串中的变量占位符
     *
     * @param template 包含${varName}格式占位符的模板字符串
     * @param variables 变量名和值的映射
     * @return 替换后的字符串
     */
    public static String replaceVariables(String template, Map<String, Object> variables) {
        if (isEmpty(template) || variables == null || variables.isEmpty()) {
            return template;
        }

        // 匹配${varName}格式的占位符
        Pattern pattern = Pattern.compile("\\$\\{([^}]+)\\}");
        Matcher matcher = pattern.matcher(template);

        StringBuffer result = new StringBuffer();

        while (matcher.find()) {
            String varName = matcher.group(1);
            Object value = variables.get(varName);

            // 如果变量值为null，保留原占位符
            String replacement = (value != null) ? value.toString() : "${" + varName + "}";

            // 处理replacement中的特殊字符，如$和\
            replacement = Matcher.quoteReplacement(replacement);

            matcher.appendReplacement(result, replacement);
        }

        matcher.appendTail(result);
        return result.toString();
    }

    /**
     * 替换字符串中的变量占位符，提供默认值
     *
     * @param template 包含${varName}格式占位符的模板字符串
     * @param variables 变量名和值的映射
     * @param defaultValue 变量不存在时的默认值
     * @return 替换后的字符串
     */
    public static String replaceVariables(String template, Map<String, Object> variables, String defaultValue) {
        if (isEmpty(template)) {
            return template;
        }

        if (variables == null || variables.isEmpty()) {
            // 如果没有提供变量，将所有占位符替换为默认值
            Pattern pattern = Pattern.compile("\\$\\{([^}]+)\\}");
            Matcher matcher = pattern.matcher(template);

            StringBuffer result = new StringBuffer();
            while (matcher.find()) {
                matcher.appendReplacement(result, Matcher.quoteReplacement(defaultValue));
            }

            matcher.appendTail(result);
            return result.toString();
        }

        // 匹配${varName}格式的占位符
        Pattern pattern = Pattern.compile("\\$\\{([^}]+)\\}");
        Matcher matcher = pattern.matcher(template);

        StringBuffer result = new StringBuffer();

        while (matcher.find()) {
            String varName = matcher.group(1);
            Object value = variables.get(varName);

            // 如果变量值为null，使用默认值
            String replacement = (value != null) ? value.toString() : defaultValue;

            // 处理replacement中的特殊字符，如$和\
            replacement = Matcher.quoteReplacement(replacement);

            matcher.appendReplacement(result, replacement);
        }

        matcher.appendTail(result);
        return result.toString();
    }

    /**
     * 首字母大写
     *
     * @param str 要处理的字符串
     * @return 首字母大写后的字符串
     */
    public static String capitalize(String str) {
        if (isEmpty(str)) {
            return str;
        }

        if (str.length() == 1) {
            return str.toUpperCase();
        }

        return Character.toUpperCase(str.charAt(0)) + str.substring(1);
    }

    /**
     * 首字母小写
     *
     * @param str 要处理的字符串
     * @return 首字母小写后的字符串
     */
    public static String uncapitalize(String str) {
        if (isEmpty(str)) {
            return str;
        }

        if (str.length() == 1) {
            return str.toLowerCase();
        }

        return Character.toLowerCase(str.charAt(0)) + str.substring(1);
    }

    /**
     * 将下划线分隔的字符串转换为驼峰命名
     * 例如：user_name -> userName
     *
     * @param str 下划线分隔的字符串
     * @return 驼峰命名的字符串
     */
    public static String toCamelCase(String str) {
        if (isEmpty(str)) {
            return str;
        }

        StringBuilder result = new StringBuilder();
        boolean nextUpper = false;

        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);

            if (c == '_') {
                nextUpper = true;
            } else {
                if (nextUpper) {
                    result.append(Character.toUpperCase(c));
                    nextUpper = false;
                } else {
                    result.append(c);
                }
            }
        }

        return result.toString();
    }

    /**
     * 将驼峰命名的字符串转换为下划线分隔
     * 例如：userName -> user_name
     *
     * @param str 驼峰命名的字符串
     * @return 下划线分隔的字符串
     */
    public static String toSnakeCase(String str) {
        if (isEmpty(str)) {
            return str;
        }

        StringBuilder result = new StringBuilder();

        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);

            if (Character.isUpperCase(c)) {
                if (i > 0) {
                    result.append('_');
                }
                result.append(Character.toLowerCase(c));
            } else {
                result.append(c);
            }
        }

        return result.toString();
    }
}
